package io.virtualapp.home.adapters;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.List;

import io.virtualapp.bean.ImageBean;

public class ImagePagerAdapter extends PagerAdapter {
    private Context context;
  //  private String url;
    private List<ImageBean> datas;
    private OnItemClickListener listener;
    public ImagePagerAdapter(Context context, List<ImageBean> datas){
        this.context = context;
      //  this.url = url;
        this.datas = datas;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return datas!=null?datas.size():0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //String urls = url+ File.separator+datas.get(position).getPath();
        PhotoView photoView = new PhotoView(context);

        if (listener!=null){
            photoView.setOnClickListener(view -> {
                listener.OnItemclick();
            });
        }
        Glide.with(context).load(datas.get(position).getPath())
                .diskCacheStrategy(DiskCacheStrategy.ALL).into(photoView);
       // ImageLoader.getInstance().loadImage(context,photoView,urls);
        container.addView(photoView);
        return photoView;
    }

    /**
     * 获取当前数据的总页数
     * @return
     */
    public int getSize(){
        return datas!=null?datas.size():0;
    }


    public interface OnItemClickListener{
         void OnItemclick();
    }
}
